


/*
*************************************************************************
*西瑞克斯研发管理系统
*版 本: 1.0   
*日 期：02/15/2012    
*作 者： 王浩 QQ：873987060   
*************************************************************************   
*/  
package com.application.yfprojectdesign.dao;
import java.util.List;
import java.util.ArrayList;
import org.apache.commons.lang.StringUtils;
import org.hibernate.Session;

import com.manager.commons.CommonUtil;
import com.manager.commons.page.PageBean;
import org.apache.log4j.Logger;
import com.manager.commons.dao.HibernateGenericDao;
import com.manager.commons.page.PageBean;
import com.application.yfprojectdesign.entity.YfProjectDesign;
import com.application.yfprojectdesign.struts.form.YfProjectDesignForm;
 /**
 * 
 * Description:YfProjectDesignDAO<br />
 * 
 * Version:V0.1<br />
 * 
 * Author: 王浩 QQ：873987060 <br />
 * 
 * History: <br />
 * 
 * 02/15/2012 
 * 
 */
public class YfProjectDesignDAO extends HibernateGenericDao<YfProjectDesign>
{
    private static final Logger logger = Logger.getLogger(YfProjectDesignDAO.class);

	private YfProjectDesign yfProjectDesign;

	public YfProjectDesign getYfProjectDesign() {
		return this.yfProjectDesign;
	}

	public void setYfProjectDesign(YfProjectDesign yfProjectDesign) {
		this.yfProjectDesign = yfProjectDesign;
	}

	public Integer getAllCountYfProjectDesign(YfProjectDesignForm yfProjectDesignForm){
	    List args = new ArrayList();
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT COUNT(0) FROM yf_project_design t left join yf_project p on t.project_no = p.project_no  left join usertable u on p.project_manager = u.id left join department b on p.project_org = b.id WHERE 1=1  ");
                sql = getStringBuffer(yfProjectDesignForm,sql,args);
		return super.getCount(sql.toString(),args.toArray());
	}
        
	public void deleteYfProjectDesigns(YfProjectDesignForm yfProjectDesignForm){    	
        String hql = " delete from YfProjectDesign where id = '" + yfProjectDesignForm.getId() + "'";
		Session session = this.getSession();
		try
		{
			session.createQuery(hql).executeUpdate();
		}
		catch(RuntimeException re)
		{
		        logger.error("deleteYfProjectDesigns failed");	
			throw re;
		}
		finally
		{
			if (session != null)
			{
				releaseSession(session);
			}
		}
       }

	public List searchYfProjectDesign(YfProjectDesignForm yfProjectDesignForm,PageBean pageBean){  
	    List args = new ArrayList();
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT t.*,u.rname rname,p.*,b.name project_org_name FROM yf_project_design t left join yf_project p on t.project_no = p.project_no  left join usertable u on p.project_manager = u.id left join department b on p.project_org = b.id WHERE 1=1  ");
		sql = getStringBuffer(yfProjectDesignForm,sql,args);
		sql.append(" order by t.create_date desc");
		return getPageInfo(pageBean,sql.toString(),args);
	}

	/**
	 * 查询条件的封装 
	 * @param departmentForm 
	 * @return
	 */
	private StringBuffer getStringBuffer(YfProjectDesignForm yfProjectDesignForm,StringBuffer buf,List args){
		/* 需要加入查询条件时封装
		*/
		
       if(StringUtils.isNotBlank(yfProjectDesignForm.getProjectNo())){
		    buf.append(" and t.PROJECT_NO like ? ");
			args.add("%" + yfProjectDesignForm.getProjectNo().trim() + "%");
		}
                				                
		if(null != yfProjectDesignForm.getAuditStatus() && yfProjectDesignForm.getAuditStatus().longValue() != 0){
	     	buf.append(" and t.AUDIT_STATUS = ? ");
			args.add(yfProjectDesignForm.getAuditStatus());
		}
		   if(StringUtils.isNotBlank(yfProjectDesignForm.getId())){
		     	buf.append(" and t.id = ? ");
				args.add(yfProjectDesignForm.getId());
			}              
		   if (StringUtils.isNotBlank(yfProjectDesignForm.getProjectName())) {
				buf.append(" and p.project_name like ? ");
				args.add("%" + yfProjectDesignForm.getProjectName().trim() + "%");
			}
			if (StringUtils.isNotBlank(yfProjectDesignForm.getProjectOrg())) {
				buf.append(" and p.project_org = ? ");
				args.add(yfProjectDesignForm.getProjectOrg());
			}
			if (null != yfProjectDesignForm.getProjectType()&& yfProjectDesignForm.getProjectType().longValue() != 0) {
				buf.append(" and p.project_type = ? ");
				args.add(yfProjectDesignForm.getProjectType());
			}
			if(yfProjectDesignForm.getDesignType()!=null){
				buf.append(" and t.design_type = ? ");
				args.add(yfProjectDesignForm.getDesignType());
			}
			if(yfProjectDesignForm.getChildType()!=null){
				buf.append(" and t.child_type = ? ");
				args.add(yfProjectDesignForm.getChildType());
			}
			 if(StringUtils.isBlank(yfProjectDesignForm.getId())){
					buf = CommonUtil.filterData(buf,args);
		   }   
		

         return buf;
	}
	
}
	